package com.xinsoft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysApproveFlowNodeDto;
import com.xinsoft.entity.dto.SysFlowParam;
import com.xinsoft.entity.po.*;
import com.xinsoft.mapper.SysApproveFlowNodeMapper;
import com.xinsoft.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lijie
 * @description 针对表【sys_approve_flow_node(审批流节点表)】的数据库操作Service实现
 * @createDate 2023-05-22 13:44:38
 */
@Service
public class SysApproveFlowNodeServiceImpl extends ServiceImpl<SysApproveFlowNodeMapper, SysApproveFlowNode>
        implements SysApproveFlowNodeService {

    @Resource
    SysApproveFlowNodeMapper nodeMapper;

    @Autowired
    SysUserService userService;

    @Autowired
    SysDeptService deptService;

    @Autowired
    SysRoleService roleService;

    @Autowired
    SysApproveNodeRelService relService;

    @Override
    public Object findPageListByParam(SysFlowParam param) {
        //分页
        QueryWrapper<SysApproveFlowNode> queryWrapper = new QueryWrapper<>();
        //查询条件
        queryWrapper.eq("flow_id", param.getFlowId())
                .orderByAsc("num");
        List<SysApproveFlowNode> list = nodeMapper.selectList(queryWrapper);
        // 根据节点候选规则类型填充 部门名，用户名，角色名
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                SysApproveFlowNode node = list.get(i);
                List<SysApproveNodeRel> listRel = relService.list(new QueryWrapper<SysApproveNodeRel>().eq("node_id", node.getId()));
                if( listRel.size() > 0){
                    List<Integer> reIdList = new ArrayList<>();
                    for (int j = 0; j < listRel.size(); j++) {
                        reIdList.add(listRel.get(j).getResourceId());
                    }
                    // 封装用户名
                    if (node.getFlowType() == 1) {
                        List<SysUser> sysUserList = userService.listByIds(reIdList);
                        if (sysUserList.size() > 0) {
                            for (int j = 0; j < sysUserList.size(); j++) {
                                if (list.get(i).getResourceName() == null) {
                                    list.get(i).setResourceName(sysUserList.get(j).getRealName());
                                } else {
                                    list.get(i).setResourceName(list.get(i).getResourceName() + ";" + sysUserList.get(j).getRealName());
                                }
                            }
                        }
                    }
                    // 封装部门名
                    if (node.getFlowType() == 3) {
                        List<SysDept> deptServices = deptService.listByIds(reIdList);
                        if (deptServices.size() > 0) {
                            for (int j = 0; j < deptServices.size(); j++) {
                                if (list.get(i).getResourceName() == null) {
                                    list.get(i).setResourceName(deptServices.get(j).getDeptName());
                                } else {
                                    list.get(i).setResourceName(list.get(i).getResourceName() + ";" + deptServices.get(j).getDeptName());
                                }
                            }
                        }
                    }
                    // 封装角色名
                    if (node.getFlowType() == 2) {
                        List<SysRole> roleList = roleService.listByIds(reIdList);
                        if (roleList.size() > 0) {
                            for (int j = 0; j < roleList.size(); j++) {
                                if (list.get(i).getResourceName() == null) {
                                    list.get(i).setResourceName(roleList.get(j).getRoleName());
                                } else {
                                    list.get(i).setResourceName(list.get(i).getResourceName() + ";" + roleList.get(j).getRoleName());
                                }
                            }
                        }
                    }
                }
            }
        }
        //返回值
        return list;
    }


    @Override
    public void add(SysApproveFlowNodeDto node, Integer num) {
        // 查看这个流程有没有其他节点
        List<SysApproveFlowNode> otherNodeList = this.list(new QueryWrapper<SysApproveFlowNode>().eq("flow_id", node.getFlowId()));
        // 第一次新增节点并且无其他节点
        if (num == null && otherNodeList.size() == 0) {
            node.setNum(Const.NODE_DEFAULT_SORT);
        }
        // 已经有节点了，在这批节点上新增
        if (num == null && otherNodeList.size() > 0) {
            for (int i = 0; i < otherNodeList.size(); i++) {
                otherNodeList.get(i).setNum(otherNodeList.get(i).getNum() + Const.NODE_OFFSET );
            }
            this.updateBatchById(otherNodeList);
            // 把它设置为第一个序列
            node.setNum(Const.NODE_DEFAULT_SORT);
        }
        // 已经有节点了，在中间节点后置新增
        if (num != null && otherNodeList.size() > 0) {
            // 先把后续的节点偏移，给这个节点腾位置
            for (int i = 0; i < otherNodeList.size(); i++) {
                if(otherNodeList.get(i).getNum() > node.getNum()){
                    otherNodeList.get(i).setNum(otherNodeList.get(i).getNum() + Const.NODE_OFFSET );
                }
                this.updateBatchById(otherNodeList);
            }
            // 把它设置好
            node.setNum(node.getNum() + Const.NODE_OFFSET);
        }
        this.save(node);
        if (node.getResourceId().length > 0) {
            List<SysApproveNodeRel> relList = new ArrayList<>();
            for (int i = 0; i < node.getResourceId().length; i++) {
                SysApproveNodeRel rel = new SysApproveNodeRel();
                rel.setNodeId(node.getId());
                rel.setResourceId(node.getResourceId()[i]);
                relList.add(rel);
            }
            relService.saveBatch(relList);
        }
    }

    @Override
    public void deleteNode(Integer id) {
        // 查看这个流程有没有其他节点
        SysApproveFlowNode node = this.getById(id);
        List<SysApproveFlowNode> otherNodeList = this.list(new QueryWrapper<SysApproveFlowNode>().
                eq("flow_id", node.getFlowId())
                .gt("num",node.getNum()));
        // 先做逻辑删除，查询时候会过滤掉已删除的
        this.removeById(id);
        if(otherNodeList != null){
            for (int i = 0; i < otherNodeList.size(); i++) {
                // 全部偏移-1
                otherNodeList.get(i).setNum(otherNodeList.get(i).getNum() - Const.NODE_OFFSET);
            }
            this.updateBatchById(otherNodeList);
        }
    }

    @Override
    public SysApproveFlowNodeDto getInfoById(Integer id) {
        SysApproveFlowNode node = this.getById(id);
        List<SysApproveNodeRel> listRel = relService.list(new QueryWrapper<SysApproveNodeRel>().eq("node_id", node.getId()));
        SysApproveFlowNodeDto dto = new SysApproveFlowNodeDto();
        List<Integer> integerList = new ArrayList<>();
        for (int i = 0; i < listRel.size(); i++) {
            integerList.add(listRel.get(i).getResourceId());
        }
        dto.setResourceId(integerList.toArray(new Integer[0]));
        BeanUtil.copyProperties(node,dto);
        return dto;
    }
}




